package com.odl.ds.pipeline;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.URLUtil;
import com.odl.ds.utils.DownLoadUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.regex.Match;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author oudl
 * @date 2025/08/06 11:22
 */
@Slf4j
public class UndrawPipeline implements Pipeline {
    // 正则表达式模式
    static String pattern = "https://cdn\\.undraw\\.co/illustration/(.+?)_";

    @Override
    public void process(ResultItems resultItems, Task task) {

        Pattern pattern = Pattern.compile(UndrawPipeline.pattern);
        List<String> links = (List<String>) resultItems.get("data");
        for (String link : links) {
            log.info(link);
            Matcher matcher = pattern.matcher(link);
            String name="";
            if (matcher.find()) {
                 name=matcher.group(1);
                try {
                    String extName = FileNameUtil.extName(URLUtil.url(link).getPath());
                    DownLoadUtils.downloadSvg(link, String.format("%s.%s", name, extName), "E:\\UI库");
                    log.info("链接下载成功！{}", link);
                } catch (Exception e) {
                    log.error("链接下载失败！{}", link);
                }
            }

        }
    }

    public static void main(String[] args) throws Exception {
        DownLoadUtils.downloadSvg("https://cdn.undraw.co/illustration/stripe-payments_jxnn.svg","1.svg","E:\\UI库");
    }
}